package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import model.GameConstants;
import model.Model;
import sim.Simulator;


@SuppressWarnings("serial")
public class SetupServerView extends CtfPanel {
	
	private Image image;
	private JPanel goButton;
	private JLabel clockMultiplierLabel;
	private JTextField clockMultiplierTextField;
	private JLabel testModeLabel;
	private JCheckBox testModeToggle;
	private JLabel serverPortLabel;
	private JTextField serverPortTextField;
	
	public SetupServerView(CtfFrame window) {
		super(window);
		this.setBackground(Color.black);
		//configure clock multiplier label
		this.clockMultiplierLabel = new JLabel("Clock Multiplier:");
		this.clockMultiplierLabel.setFont(new Font("Arial", Font.BOLD, 20));
		this.clockMultiplierLabel.setForeground(Color.white);
		this.add(this.clockMultiplierLabel);
		//configure clock multiplier text field
		this.clockMultiplierTextField = new JTextField();
		this.clockMultiplierTextField.setBorder(null);
		this.clockMultiplierTextField.setText("1");
		this.clockMultiplierTextField.setSize(140, 20);
		this.clockMultiplierTextField.setFont(new Font("Arial", Font.BOLD, 20));
		this.clockMultiplierTextField.setHorizontalAlignment(SwingConstants.CENTER);
		this.clockMultiplierTextField.setColumns(5);
		this.add(this.clockMultiplierTextField);
		//configure test mode label
		this.testModeLabel = new JLabel("Test Mode:");
		this.testModeLabel.setFont(new Font("Arial", Font.BOLD, 20));
		this.testModeLabel.setForeground(Color.white);
		this.add(this.testModeLabel);
		//configure test mode label
		this.testModeToggle = new JCheckBox();
		this.testModeToggle.setBorder(null);
		this.testModeToggle.setSelected(GameConstants.TEST_MODE);
		this.add(this.testModeToggle);
		//configure server port label
		this.serverPortLabel = new JLabel("Server Port#:");
		this.serverPortLabel.setFont(new Font("Arial", Font.BOLD, 20));
		this.serverPortLabel.setForeground(Color.white);
		this.add(this.serverPortLabel);
		//configure server port text field
		this.serverPortTextField = new JTextField();
		this.serverPortTextField.setBorder(null);
		this.serverPortTextField.setText(Integer.toString(GameConstants.SERVER_PORT));
		this.serverPortTextField.setSize(140, 20);
		this.serverPortTextField.setFont(new Font("Arial", Font.BOLD, 20));
		this.serverPortTextField.setHorizontalAlignment(SwingConstants.CENTER);
		this.serverPortTextField.setColumns(6);
		this.add(this.serverPortTextField);
		//configure go button
		this.goButton = new GoButton();
		this.add(this.goButton);
		try {
			this.image = javax.imageio.ImageIO.read(new java.net.URL(getClass()
					.getResource("/img/lotties_dark.jpg"), "lotties_dark.jpg"));
		} catch (Exception e) { /* handled in paintComponent() */
		}
		this.setVisible(true);
	}
	
	private void go() {
		try {
			double clockMultiplier = Double.parseDouble(clockMultiplierTextField.getText());
			if (clockMultiplier > 0) {
				GameConstants.CLOCK_MULTIPLIER = clockMultiplier;
				GameConstants.TEST_MODE = testModeToggle.isSelected();
				Model model = new Model();
				GameView view = new GameView(getWindow(), model);
				@SuppressWarnings("unused")
				Simulator sim = new Simulator(model);
				setView(view);
			}
		} catch(Exception e) {
			//nothing
		}
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		this.clockMultiplierLabel.setLocation((int)(getWidth()*0.40), (int)(getHeight()*0.15));
		this.clockMultiplierTextField.setLocation((int)(getWidth()*0.4 + 160), (int)(getHeight()*0.15));
		this.testModeLabel.setLocation((int)(getWidth()*0.45), (int)(getHeight()*0.25));
		this.testModeToggle.setLocation((int)(getWidth()*0.45 + 130), (int)(getHeight()*0.25 + 5));
		this.serverPortLabel.setLocation((int)(getWidth()*0.40), (int)(getHeight()*0.35));
		this.serverPortTextField.setLocation((int)(getWidth()*0.40 + 150), (int)(getHeight()*0.35));
		this.goButton.setBounds((int)(getWidth()*0.75), (int)(getHeight()*0.85), (int)(getWidth()*0.2), (int)(getHeight()*0.15));
		if (this.image != null)
			g.drawImage(image, 0, 0, this.getWidth(), (int)(0.85*this.getHeight()), this);
	}
	
	public class GoButton extends JPanel implements MouseListener {
		private Image normalImage;
		private Image hoverImage;
		private Image currentImage;
		public GoButton() {
			this.addMouseListener(this);
			try {
				this.normalImage = javax.imageio.ImageIO.read(new java.net.URL(getClass()
						.getResource("/img/go.png"), "go.png"));
			} catch (Exception e) { /* handled in paintComponent() */
			}
			try {
				this.hoverImage = javax.imageio.ImageIO.read(new java.net.URL(getClass()
						.getResource("/img/go_hover.png"), "go_hover.png"));
			} catch (Exception e) { /* handled in paintComponent() */
			}
			currentImage = normalImage;
		}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			//nothing
		}
		@Override
		public void mouseEntered(MouseEvent arg0) {
			this.currentImage = this.hoverImage;
			this.repaint();
		}
		@Override
		public void mouseExited(MouseEvent arg0) {
			this.currentImage = this.normalImage;
			this.repaint();
		}
		@Override
		public void mousePressed(MouseEvent arg0) {
			//nothing
		}
		@Override
		public void mouseReleased(MouseEvent arg0) {
			go();
		}
		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			if (this.currentImage != null)
				g.drawImage(this.currentImage, 0, 0, this.getWidth(), this.getHeight(), this);
		}

	}

}
